package com.example.demo.controller;

import com.example.demo.model.Userinfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @Transactional(propagation = Propagation.NESTED)
    public int add(){
        //1、非空判断
        Userinfo userinfo=new Userinfo();
        userinfo.setUsername("李四");
        userinfo.setPassword("123");
        //2、调用service执行添加
        int result=userService.add(userinfo);
        System.out.println("result："+result);
        
        userService.insert(userinfo);

//        try {
//            int num=10/0;
//        }catch (Exception e){
////            throw e;
//            //手动回滚事务
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }

        //3、将结果给前端
        return result;
    }
}
